#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import os
import platform

from common.common import execute_cmd
from common.logger import Logger

LOGGER = Logger().get_logger(filename="db2.log")


def get_system():
    # 获取系统名称
    system_name = platform.system()

    if system_name == "AIX":
        return "AIX"
    elif system_name == "Linux":
        # Check for Red Hat or derivatives (e.g. CentOS)
        if os.path.exists("/etc/redhat-release"):
            return "Red Hat"
        else:
            return "Other Linux distribution"
    else:
        return "Other OS"


def check_ha_cluster_node_right():
    system_type = get_system()
    LOGGER.info(f"system_type:{system_type}")
    if system_type == "AIX":
        var_cmd = "clstat -o"
        if os.path.exists("/usr/sbin/cluster/clstat"):
            var_cmd = "/usr/sbin/cluster/clstat -o"
        if os.path.exists("/usr/es/sbin/cluster/clstat"):
            var_cmd = "/usr/es/sbin/cluster/clstat -o"
        if system_type == "Red Hat":
            var_cmd = "pcs status"
    elif system_type == "Red Hat":
        var_cmd = "pcs status"
    else:
        raise Exception("Unsupported system type.")
    ret, cont, err = execute_cmd(var_cmd)
    LOGGER.info(f"ret:{ret}, cont:{cont}, err:{err}")
    ret_const = cont if ret else err
    return ret, ret_const
